package system

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type SysConfig struct {
	ConfigId   int64  `orm:"column(config_id);pk"`
	ConfigKey  string `description:"配置key"`
	ConfigVal  string `description:"配置值"`
	ConfigKind string `description:"配置类型：'文本','开关'"`
	Remark     string `description:"备注信息"`
	CreateTime time.Time
	ModifyTime time.Time
}

type SysConfigView struct {
	ConfigId   int64  `description:"配置id"`
	ConfigKey  string `description:"配置key"`
	ConfigVal  string `description:"配置值"`
	ConfigKind string `description:"配置类型：'文本','开关'"`
	Remark     string `description:"备注信息"`
	CreateTime string `description:"创建时间"`
	ModifyTime string `description:"修改时间"`
}

// 新增
func (m *SysConfig) Add() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(m)
	return
}

func (r *SysConfig) GetSysConfigAll() (items []*SysConfigView, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM sys_config
			WHERE 1=1 `
	sql += ` ORDER BY create_time DESC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type SysConfigAllResp struct {
	List []*SysConfigView
}

// 修改
func (m *SysConfig) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrm()
	ptrStructOrTableName := "sys_config"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

type SysConfigEditReq struct {
	ConfigKey string `description:"配置key"`
	ConfigVal string `description:"配置值"`
	Remark    string `description:"备注信息"`
}

func (r *SysConfig) GetSysConfigByConfigKey(configKey string) (items *SysConfigView, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM sys_config
			WHERE config_key=? `
	err = o.Raw(sql, configKey).QueryRow(&items)
	return
}
